package com.koron.device.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.common.core.util.DisposeBeanUtil;
import com.koron.device.bean.EquipmentChangeQuery;
import com.koron.device.bean.EquipmentChangeVO;
import com.koron.device.filter.DeviceFilter;
import com.koron.device.service.EquipmentChangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author qingfeng
 * @description: 设备异动
 * @date 2021/7/14 15:34
 */
@Slf4j
@RequestMapping("/equipmentChange")
@RestController
@Api(tags = "设备异动")
public class EquipmentChangeController implements WorkflowController {

    @Autowired
    private EquipmentChangeService service;

    @Autowired
    private DeviceFilter filter;

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增或修改变")
    public Response update(@CasUser @ApiIgnore EamUser user,@RequestBody EquipmentChangeVO equipment){
        DisposeBeanUtil.dispose(user, equipment);
        EquipmentChangeVO vo = ADOConnection.runTask(user.getCurrDs(), service, "saveOrUpdate", EquipmentChangeVO.class, equipment);
        return Response.ok(vo);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除变更单")
    public Response delete(@CasUser @ApiIgnore EamUser user,String id){
        Boolean flag = ADOConnection.runTask(user.getCurrDs(), service, "delete", Boolean.class, id);
        return Response.ok(flag);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "查询单个变更单")
    public Response<EquipmentChangeVO> getById(@CasUser @ApiIgnore EamUser user,String id){
        EquipmentChangeVO vo = ADOConnection.runTask(user.getCurrDs(), service, "getById", EquipmentChangeVO.class, id);
        return Response.ok(vo);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询分页列表")
    public Response<PageInfo<EquipmentChangeVO>> list(@CasUser @ApiIgnore EamUser user, EquipmentChangeQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<EquipmentChangeVO> list = ADOConnection.runTask(user.getCurrDs(), service, "list", List.class, query);
        return Response.ok(new PageInfo<>(list));
    }

    @Override
    @ApiOperation("修改业务状态, 非前端接口")
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("workflowUpdate = {}", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), service, "workflowUpdate", Boolean.class, redirectQuery);
        return Response.success();
    }

    /**
     * 结束时的修改方法，可重写后自定义
     */
    @Override
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("updateEnd = {}", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), service, "updateEnd", Boolean.class, redirectQuery);
        return Response.ok();
    }

    /**
     * 自动过滤
     * 点击工作流提交的时候，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/submitter")
    public Response<List<WorkflowUser>> submitter(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("设备异动设置审批人入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Submitter", List.class, submitterParam);
        return Response.ok(users);
    }

    /**
     * 选人组件过滤
     * 点击选人组件时，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/handler")
    public Response<List<WorkflowUser>> handler(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("设备异动设选人组件过滤入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Handler", List.class, submitterParam);
        return Response.ok(users);
    }
}
